/**
 * 
 */
Ext.ns('Ext.lams');
/***************************************************************/
//var ageTest=/^\d+$/;
//Ext.apply(Ext.form.VTypes,{
//	"age":function(_str){
//		if(ageTest.test(_str) && parseInt(_str)<200)
//			return true;
//		else return false;
//	},
//	"ageText":"",
//	"ageMask":/^\d+$/
//});
/***************************************************************/
FunctionInfoPanel = Ext.extend(Ext.form.FormPanel,{
	constructor:function(_cfg){
		if(_cfg == null)
			_cfg = {}; 
		Ext.apply(this,_cfg);
		var _readOnly = this.readOnly==null ? false : this.readOnly;
		FunctionInfoPanel.superclass.constructor.call(this,{
			defaultType:"textfield",
			defaults:{anchor:"95%",readOnly:_readOnly},
		//	frame:true,
			labelWidth:65,
			baseCls:"x-plain",
			items:[{
				fieldLabel:"模块名称",
				allowBlank:_readOnly,
				blankText:"该项为必填项",
				name:"module.moduleName"
			},{
				fieldLabel:"链接地址",
				allowBlank:_readOnly,
				blankText:"该项为必填项",
				name:"href"
			},{
				fieldLabel:"功能名称",
				allowBlank:_readOnly,
				blankText:"该项为必填项",
				name:"functionName"
			}],
			getValue:function(){
				if(this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
//					alert(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			},
			setValue:function(_r){
				this.getForm().loadRecord(_r);
			},
			clear:function(){
				this.getForm().reset();
			}
		});
	}
});
/****************************************************************/
FunctionInfoViewPanel = Ext.extend(FunctionInfoPanel,{
	constructor:function(){
		FunctionInfoViewPanel.superclass.constructor.call(this,{
			renderTo:Ext.getBody(),
			width:350,
			readOnly:true
		})
	}
});
/****************************************************************/
FunctionInfoWindow = Ext.extend(Ext.Window,{
	form:null,
	constructor:function(){
		this.form = new FunctionInfoPanel();
		FunctionInfoWindow.superclass.constructor.call(this,{
			plain:true,
			width:350,
			closeAction:"hide",
			items:this.form,
			modal:true,
			buttons:[{
				//text:"确定",
                xtype:"btn_submit",
				handler:this.onSubmitClick,
				scope:this
			},{
				//text:"取消",
                xtype:"btn_cancel",
				handler:this.onCancelClick,
				scope:this
			}]
		});
		this.addEvents("submit");
	},
	close:function(){
		this.form.clear();
	//	this.removeListener('submit');
		this.hide();
	},
	onSubmitClick:function(){
		try{
			this.fireEvent("submit",this,this.form.getValue());
		}catch(_r){
			return;
		}
		this.close();
	},
	onCancelClick:function(){
		this.close();
	}
});
/*****************************************************************/
InsertFunctionInfoWindow = Ext.extend(FunctionInfoWindow,{
	title:"添加功能"
});
UpdateFunctionInfoWindow = Ext.extend(FunctionInfoWindow,{
	title:"修改功能",
	load:function(_r){
		this.form.setValue(_r);
	}
});
/*****************************************************************/
var _store = new Ext.data.JsonStore({
	//	autoLoad:true,
	proxy:new Ext.data.HttpProxy({
		url:"findFunction.action?m.moduleId=1",
		method:"GET"
	}),
	totalProperty:"totalCount",
	root:"data",
	fields:["module.moduleName","href","functionName"]
});

Ext.lams.FunctionGridPanel = Ext.extend(Ext.grid.GridPanel,{
	insertWin:new InsertFunctionInfoWindow(),
	updateWin:new UpdateFunctionInfoWindow(),
	constructor:function(_cfg){
		if(!_cfg)
			_cfg = {};
		Ext.apply(this,_cfg);
		
		Ext.lams.FunctionGridPanel.superclass.constructor.call(this,{
			frame:true,
			enableColumnMove:false,
			enableColumnHide:false,
			store:_store,
			loadMask:{msg:'正在载入数据,请稍等...'},
			tbar:[{
				//text:"添加功能",
                xtype:"btn_add",
				handler:function(){
					this.insertWin.show();
				},
				scope:this
			},"-",{
				//text:"修改功能",
                xtype:"btn_edit",
				handler:function(){
					this.updateWin.show();
					try{
						this.updateWin.load(this.getSelected());
					}catch(_err){
						Ext.Msg.alert("提示",_err.message);
						this.updateWin.close();
					}
				},
				scope:this
			},"-",{
				//text:"删除功能",
                xtype:"btn_delete",
				handler:function(){
					Ext.Msg.confirm("提示","确认删除选中记录吗",this.onRemoveFunction,this);
				},
				scope:this
			}],
			bbar:new Ext.PagingToolbar({
				store:_store,
				pageSize:5,
			//	prependButtons: true,
				displayInfo : true,
				items:[
					
				]
			}),
			colModel:new Ext.grid.ColumnModel([
					{
						header:"模块名称",
						dataIndex:"module.moduleName",
						align:"center"
					},
					{header:"链接地址"},
					{header:"功能名称"}
					]),
			
			selModel:new Ext.grid.RowSelectionModel({
				singleSelect:true,
				listeners:{
					"rowselect":{
						fn:this.onRowSelected,
						scope:this
					}
				}
			})
		});
		this.insertWin.on("submit",this.onInsertWinSubmit,this);
		this.updateWin.on("submit",this.onUpdateWinSubmit,this);
		this.addEvents("rowselect");
		_store.load({params:{start:0,limit:20}});
	},
	getSelected:function(){
		var _sel = this.getSelectionModel();
		if(_sel.getCount() == 0)
			throw Error("未选中某条记录");
//		if(_sel.getCount() > 1)
//			throw Error("请选一条记录进行修改");
		return _sel.getSelected();
	},
	insert:function(_r){
		this.getStore().add(_r);
	},
	update:function(_r){
		try{
			var _sr = this.getSelected();
			var _data = _sr.data;
			for(var _i in _data){
				_sr.set(_i,_r.get(_i));
			}
			_sr.commit();
			this.fireEvent("rowselect",_sr);
		}catch(_err){
			Ext.Msg.alert("提示",_err.message);
		}
	},
	removeFunction:function(){
		try{
			this.getStore().remove(this.getSelected());
			this.fireEvent("rowselect",
					new Ext.data.Record({
						"module.moduleName":"",
						"href":"",
						"functionName":""
					}));
		}catch(_err){
			Ext.Msg.alert("提示",_err.message);
		}
	},
	onUpdateWinSubmit:function(_win,_r){
		this.update(_r);
	},
	onInsertWinSubmit:function(_win,_r){
		this.insert(_r);
	},
	onRemoveFunction:function(_btn){
		if(_btn == "yes") this.removeFunction();
	},
	onRowSelected:function(_sel,_index,_r){
		this.fireEvent("rowselect",_r);
	}
});
